#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h> 
#include <stdlib.h>       
#include <string.h>
#include <pthread.h>
//#include <sys/sem.h>
#include <errno.h>
#include <sys/ipc.h>
//#include <sys/msg.h>
#include <sys/timeb.h>
#include <sys/socket.h>
//#include <sys/socketvar.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <semaphore.h>
#include <string.h>
#include <termios.h>
#include <linux/input.h>
#include <semaphore.h>
#include <linux/shm.h>
#include <asm/unistd.h>
#include <sys/poll.h>
#include <netdb.h>

#include "../common.h"

static const int maxConfigNum = 128;
static const char defaultLogdir[] = "/data/edu/log";
extern SystemParam *pgSystemParam;

int readConfigFile(const char *configFileDir)
{
		//printf("into readConfigFile: SOFTWARE_VERSION_VALUE = %s\n", SOFTWARE_VERSION_VALUE);
		if(configFileDir == NULL)
		{
				printf("configFileDir is NULL ! {%s,%d}\n", __FILE__, __LINE__);
				return -1;	
		}
		
		ITEM *items;
		items = (ITEM*)malloc(sizeof(ITEM) * maxConfigNum);
		if(items == NULL)
		{
				printf("malloc items memory fail ! {%s,%d}\n", __FILE__, __LINE__);
				return -2;
		}
		
		int realItemNum;
		int ret = file_to_items(configFileDir, items, &realItemNum);
		if(ret < 0)
		{
				printf("read configure file fail ! ret = %d, {%s,%d}\n", ret,__FILE__,__LINE__);	
				clearConfigMemory(items, realItemNum);
				return -3;
		}
		
		if(realItemNum == 0)
		{
				printf("big mistake ! realItemNum == 0 ! prepare to download default config file to replace !\n");	
#ifdef RECOVER_CONFIG_FILE
				system("mv /data/edu/terminal.config /data/edu/terminal.config.mistake_save");
#endif
				return -99;
		}
		
		ret = putConfigureDataIntoMemory(items, maxConfigNum, realItemNum);
		if(ret < 0)
		{
				printf("putConfigureDataIntoMemory fail ! ret = %d, {%s,%d}\n", ret,__FILE__,__LINE__);	
				clearConfigMemory(items, realItemNum);
				return -4;
		}
		
		clearConfigMemory(items, realItemNum);
		
		//testSysStat();
		//testWrite(0, "1235.coms");
		return 0;
}

static int clearConfigMemory(ITEM *items, int realItemNum)
{
		if(!items) return -1;
			
		ITEM *pItem;
		for(int i = 0; i < realItemNum; i ++)
		{
				pItem = items + i;
				if(!pItem) 
				{
						char buf[128];
						sprintf(buf, "clearConfigMemory error ! pItem==NULL ! i = %d\n", i);
						LOGD(buf);
						continue;
				}
				if(pItem->key) free(pItem->key);
				if(pItem->value) free(pItem->value);
		}
		
		free(items);
		return 0;
}

static int putConfigureDataIntoMemory(ITEM *items, int maxItemNum, int realItemNum)
{
		if(pgSystemParam == NULL) 
		{
				printf("pgSystemParam == NULL ! {%s,%d} \n", __FILE__,__LINE__);
				return -1;
		}
				
		//in order to use writeLog function, init log file directory first
		if(initLog(items, realItemNum) < 0)
		{
				printf("initLog fail ! {%s,%d}\n", __FILE__, __LINE__);
				return -2;
		}
				
		if(realInitConfig(items, realItemNum) < 0)
		{
				printf("realInitConfig fail ! {%s,%d}\n", __FILE__, __LINE__);
				return -3;
		}
		
		setDefaultValue();
		
		//for test
		printf("final:\n");
		printf("logdir = %s\n", pgSystemParam->cLogsDir);
		printf("broadcastIp = %s\n", pgSystemParam->broadcastServerIp);
		printf("centerProxyIp = %s\n", pgSystemParam->centerProxyServerIp);
		for(unsigned char i = 0; i < MAX_WINDOWS_SERVER_IP_NUM; i ++)
		{
				printf("windowsIp[%d] = %s\n", i,pgSystemParam->windowsServerIp[i]);
		}
		printf("maxRemoteCtrlClient = %d\n", pgSystemParam->maxRemoteCtrlClient);
		printf("maxMicroClassClient = %d\n", pgSystemParam->maxMicroClassClient);
		printf("device name = %s\n", pgSystemParam->deviceName);
		
		printf("pgSystemParam->projectorOpenCodeNum = %d\n", pgSystemParam->projectorOpenCodeNum);
		printf("code = ");
		for(unsigned char i = 0; i < pgSystemParam->projectorOpenCodeNum; i ++)
		{
				printf("%x ", pgSystemParam->projectorOpenCode[i]);	
		}
		printf("\n");
		printf("pgSystemParam->projectorCloseCodeNum = %d\n", pgSystemParam->projectorCloseCodeNum);
		printf("code = ");
		for(unsigned char i = 0; i < pgSystemParam->projectorCloseCodeNum; i ++)
		{
				printf("%x ", pgSystemParam->projectorCloseCode[i]);	
		}
		printf("\n");
		printf("pgSystemParam->projectorVgaCodeNum = %d\n", pgSystemParam->projectorVgaCodeNum);
		printf("code = ");
		for(unsigned char i = 0; i < pgSystemParam->projectorVgaCodeNum; i ++)
		{
				printf("%x ", pgSystemParam->projectorVgaCode[i]);	
		}
		printf("\n");
		printf("pgSystemParam->projectorHdmiCodeNum = %d\n", pgSystemParam->projectorHdmiCodeNum);
		printf("code = ");
		for(unsigned char i = 0; i < pgSystemParam->projectorHdmiCodeNum; i ++)
		{
				printf("%x ", pgSystemParam->projectorHdmiCode[i]);	
		}
		printf("\n");
		
		printf("pgSystemParam->projectorBaud = %d\n", pgSystemParam->projectorBaud);
		printf("pgSystemParam->projectorCutOffElectricityDelayTime = %u\n", pgSystemParam->projectorCutOffElectricityDelayTime);
		printf("pgSystemParam->projectorSendOpenCodeDelayTime = %u\n", pgSystemParam->projectorSendOpenCodeDelayTime);
		printf("pgSystemParam->projectorParitycheck = %u\n", pgSystemParam->projectorParitycheck);
		printf("pgSystemParam->projectorManufactory = %s\n", pgSystemParam->projectorManufactory);
		printf("pgSystemParam->projectorModel = %s\n", pgSystemParam->projectorModel);
		
		return 0;
}

static int initLog(ITEM *items, int realItemNum)
{
		bool findOut = false;
		ITEM *pItem;
		char *key;
		char *value;
		
		for(int i = 0; i < realItemNum; i ++)
		{
				pItem = items + i;
				key = pItem->key;
				value = pItem->value;
				
				if(strcmp(key, "log_dir") == 0)
				{
						findOut = true;
						if(strlen(value) > 0)
						{
								strcpy(pgSystemParam->cLogsDir, value);
						}
						else
						{
								strcpy(pgSystemParam->cLogsDir, defaultLogdir);
						}
						break;
				}
		}
		if(findOut == false)
		{
				printf("there is no log_dir in config file !\n");	
				strcpy(pgSystemParam->cLogsDir, defaultLogdir);
		}
		
		if( pthread_mutex_init (&(pgSystemParam->pmtLogLock),NULL) != 0   ) 
		{
				printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
				return -1;
		}	

		return 0;
}

static int realInitConfig(ITEM *items, int realItemNum)
{
		ITEM *pItem;
		char *key;
		char *value;
		char logBuf[256];		
				
		for(int i = 0; i < realItemNum; i ++)
		{
				pItem = items + i;
				key = pItem->key;
				value = pItem->value;
				
				char *pIndex = NULL;
				unsigned char index = 0;
				if(pIndex = strstr(key, "windows_server_ip_"))
				{
						if(strlen(value) > 0)
						{	
								pIndex += strlen("windows_server_ip_");
								index = (unsigned char)atoi(pIndex) - 1;
								readMultConnectServerConfigInfo(value, _windowsServer, index);
								printf("ip value = %s, index = %d\n", value, index);
						}
				}
				else if(strcmp(key, "broadcast_server_ip") == 0)
				{
						readConnectServerConfigInfo(value, _broadcastServer);
				}
				else if(strcmp(key, "center_proxy_server_ip") == 0)
				{
						readConnectServerConfigInfo(value, _centerProxyServer);
				}	
				else if(strcmp(key, "input_time_out") == 0)
				{
						
				}
				else if(strcmp(key, "max_remote_control_client") == 0)
				{
						pgSystemParam->maxRemoteCtrlClient = atoi(value);
				}
				else if(strcmp(key, "max_mico_class_client") == 0)
				{
						pgSystemParam->maxMicroClassClient = atoi(value);
				}
				else if(strcmp(key, "device_name") == 0)
				{
						pthread_mutex_lock(&(pgSystemParam->deviceNameLock));	
						strcpy(pgSystemParam->deviceName, value);
						pthread_mutex_unlock(&(pgSystemParam->deviceNameLock));
				}
				else if(strcmp(key, "projector_open_code") == 0)
				{
						readProjectorCtrlCode(value, _open);
				}
				else if(strcmp(key, "projector_close_code") == 0)
				{
						readProjectorCtrlCode(value, _close);
				}
				else if(strcmp(key, "projector_vga_code") == 0)
				{
						readProjectorCtrlCode(value, _vga);
				}
				else if(strcmp(key, "projector_hdmi_code") == 0)
				{
						readProjectorCtrlCode(value, _hdmi);
				}
				else if(strcmp(key, "projector_baud") ==  0)
				{
						pgSystemParam->projectorBaud = atoi(value);
				}
				else if(strcmp(key, "cloud_edu_ip_config_dir") ==  0)
				{
						strcpy(pgSystemParam->cloudEduIpConfigDir, value);
				}
				else if(strcmp(key, "projector_close_delay_time") ==  0)
				{
						pgSystemParam->projectorCutOffElectricityDelayTime = atoi(value);
				}
				else if(strcmp(key, "projector_open_delay_time") ==  0)
				{
						pgSystemParam->projectorSendOpenCodeDelayTime = atoi(value);
				}
				else if(strcmp(key, "projector_parity_check") == 0)
				{
						pgSystemParam->projectorParitycheck = atoi(value);
				}
				else if(strcmp(key, "projector_manufactory") == 0)
				{
						strcpy(pgSystemParam->projectorManufactory, value);
				}
				else if(strcmp(key, "projector_model") == 0)
				{
						strcpy(pgSystemParam->projectorModel, value);
				}
				else if(isIgnoreKey(key) == false)
				{
						sprintf(logBuf, "unknow configure info ! key = %s, value = %s\n", key,value);
						writeLog(logBuf, __FILE__, __LINE__);	
				}
		}	
		
		return 0;
}

static bool isIgnoreKey(const char *key)
{
		static const char *ignoreKeyList[]	= 
		{
				"terminal_software_version", "log_dir", "update_file_dir",
				"app_software_version", "so_software_version",
				"app_update_server_ip_or_domain_name", "app_cloud_edu_html", "app_online_watch_html"
		};
		
		static const int maxNum = sizeof(ignoreKeyList) / sizeof(const char *);
		
		bool result = false;
		for(int i = 0; i < maxNum; i ++)
		{
				if(strcmp(key, ignoreKeyList[i]) == 0)
				{
						result = true;
						//printf("match ignore key list ! key = %s\n", key);
						break;	
				}
		}
		
		return result;
}

static int readConnectServerConfigInfo(char *value, ConnectionServerIpIndex index)
{		
		static char logBuf[80];
		struct hostent *host;
		if(pgSystemParam == NULL || value == NULL) 
		{
				sprintf(logBuf, "pgSystemParam=%d, value=%d\n", pgSystemParam,value);	
				writeLog(logBuf, __FILE__, __LINE__);
				return -1;
		}
			
		if(index >= _connectionServerIpMax)
		{
				sprintf(logBuf, "index >= _connectionServerIpMax, index=%d\n", index);	
				writeLog(logBuf, __FILE__, __LINE__);
				return -2;
		}
			
		static char *serverIps[] = {pgSystemParam->centerProxyServerIp, pgSystemParam->broadcastServerIp};
		static pthread_mutex_t *locks[] = {&(pgSystemParam->centerProxyServerIpLock), &(pgSystemParam->broadcastServerIpLock)};
		
		unsigned int pointerNum = sizeof(serverIps)/sizeof(char*);
		if(pointerNum != _connectionServerIpMax)
		{
				sprintf(logBuf, "pointerNum != _connectionServerIpMax, pointerNum=%d, _connectionServerIpMax=%d\n", pointerNum,_connectionServerIpMax);	
				writeLog(logBuf, __FILE__, __LINE__);
				return -3;
		}
		
		char *serverIp = serverIps[index];
		pthread_mutex_t *lock = locks[index];
		
		pthread_mutex_lock (lock);
		strcpy(serverIp, value);
		pthread_mutex_unlock (lock);
		
		return 0;
}

static int readMultConnectServerConfigInfo(char *value, MultConnectionServerIpIndex index, unsigned char sonIndex)
{		
		static char logBuf[80];
		struct hostent *host;
		if(pgSystemParam == NULL || value == NULL) 
		{
				sprintf(logBuf, "pgSystemParam=%d, value=%d\n", pgSystemParam,value);	
				writeLog(logBuf, __FILE__, __LINE__);
				return -1;
		}
			
		if(index >= _multConnectionServerIpMax)
		{
				sprintf(logBuf, "index >= _connectionServerIpMax, index=%d\n", index);	
				writeLog(logBuf, __FILE__, __LINE__);
				return -2;
		}
		
		static TypeC256 *multServerIps[] = {pgSystemParam->windowsServerIp};
		static pthread_mutex_t *locks[] = {pgSystemParam->windowsServerIpLock}; 

		static const unsigned char pointerNum = sizeof(multServerIps)/sizeof(char **);
		if(pointerNum != _multConnectionServerIpMax)
		{
				sprintf(logBuf, "pointerNum != _multConnectionServerIpMax, pointerNum=%d, _connectionServerIpMax=%d\n", pointerNum,_connectionServerIpMax);	
				writeLog(logBuf, __FILE__, __LINE__);
				return -3;
		}
		
		char *serverIp = multServerIps[index][sonIndex];	
		pthread_mutex_t *lock = locks[index] + sonIndex;
		
		pthread_mutex_lock (lock);
		strcpy(serverIp, value);
		pthread_mutex_unlock (lock);
		
		return 0;
}

int writeSoftwareVersionToCfg(const char *version)
{
		printf("into writeSoftwareVersionToCfg: version = %s\n", version);
		int ret = writeOneConfigValue(SOFTWARE_VERSION_KEY, version);
		//printf("out writeSoftwareVersionToCfg\n");
		return ret;
}

void setDefaultValue()
{
		char logBuf[256];
		if(pgSystemParam->maxRemoteCtrlClient == 0)
		{
				const unsigned char defaultValue = 5;
				sprintf(logBuf, "setDefaultValue: pgSystemParam->maxRemoteCtrlClient == 0 ! set this  value to default value %d\n", defaultValue);	
				LOGD(logBuf);
				pgSystemParam->maxRemoteCtrlClient = defaultValue;
		}
		
		if(pgSystemParam->projectorCutOffElectricityDelayTime < 10 || pgSystemParam->projectorCutOffElectricityDelayTime > 255)
		{
				const unsigned int defaultValue = 180;
				sprintf(logBuf, "setDefaultValue: pgSystemParam->projectorCutOffElectricityDelayTime error ! before=%d, set this  value to default value %d\n", pgSystemParam->projectorCutOffElectricityDelayTime,defaultValue);	
				LOGD(logBuf);
				pgSystemParam->projectorCutOffElectricityDelayTime = defaultValue;	
		}
		
		if(pgSystemParam->projectorSendOpenCodeDelayTime < 2 || pgSystemParam->projectorSendOpenCodeDelayTime > 255)
		{
				const unsigned int defaultValue = 20;
				sprintf(logBuf, "setDefaultValue: pgSystemParam->projectorSendOpenCodeDelayTime error ! before=%d,set this  value to default value %d\n", pgSystemParam->projectorSendOpenCodeDelayTime,defaultValue);	
				LOGD(logBuf);
				pgSystemParam->projectorSendOpenCodeDelayTime = defaultValue;	
		}
		
		if(pgSystemParam->projectorParitycheck > MAX_PARIRY)
		{
				const unsigned int defaultValue = 0;
				sprintf(logBuf,"pgSystemParam->projectorParitycheck error ! before=%d, MAX_PARIRY=%d,set defaultValue=%d\n", pgSystemParam->projectorParitycheck,MAX_PARIRY,defaultValue);
				LOGD(logBuf);
				pgSystemParam->projectorParitycheck = defaultValue;	
		}
		
		for(int i=0;i<MAX_WINDOWS_SERVER_IP_NUM;i++) pgSystemParam->restartUsbClientConnect[i] = false;
}

int initOther()
{
		//printf("into initOther\n");
		if( pthread_mutex_init (&(pgSystemParam->pmtCfgLock),NULL) != 0   ) 
		{
				printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
				return -1;
		}	
		
		for(unsigned char i = 0; i < MAX_WINDOWS_SERVER_IP_NUM; i ++)
		{
				if( pthread_mutex_init(&(pgSystemParam->windowsServerIpLock[i]),NULL) != 0   ) 
				{
						printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
						return -2;
				}	
		}
		
		if( pthread_mutex_init(&(pgSystemParam->broadcastServerIpLock), NULL) != 0   ) 
		{
				printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
				return -3;
		}	
		
		if( pthread_mutex_init(&(pgSystemParam->centerProxyServerIpLock), NULL) != 0   ) 
		{
				printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
				return -4;
		}	
		
		if( pthread_mutex_init(&(pgSystemParam->projectorCodeLock), NULL) != 0   ) 
		{
				printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
				return -5;
		}
		
		if( pthread_mutex_init(&(pgSystemParam->deviceNameLock), NULL) != 0   ) 
		{
				printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
				return -6;
		}
		
		if( pthread_mutex_init(&(pgSystemParam->accessCardServerDomainNameLock), NULL) != 0   ) 
		{
				printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
				return -7;
		}	
		
		//if( pthread_mutex_init(&(pgSystemParam->projector_WirelessMic_ttyS_Lock), NULL) != 0   ) 
		//{
		//		printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
		//		return -8;
		//}	
		
		//printf("exit initOther\n");
		return 0;
}

static int readProjectorCtrlCode(char *value, ProjectorActionWay actionWay)
{
		char logBuf[256];
		if(!value)
		{
				writeLog("value == NULL ! ", __FILE__, __LINE__);
				return -1;
		}
		
		if(actionWay >= _projectorActionWayMax)
		{
				sprintf(logBuf, "actionWay >= _projectorActionWayMax ! actionWay=%d,_projectorActionWayMax=%d\n", actionWay,_projectorActionWayMax);	
				writeLog(logBuf, __FILE__, __LINE__);
				return -2;
		}
	
		unsigned char *saveCode = NULL;
		unsigned char *saveCodeNum = NULL;
		CtrlCodeType code;
		switch(actionWay)
		{
				case _open:
					saveCode = pgSystemParam->projectorOpenCode;
					saveCodeNum = &(pgSystemParam->projectorOpenCodeNum);
					break;
				case _close:
					saveCode = pgSystemParam->projectorCloseCode;
					saveCodeNum = &(pgSystemParam->projectorCloseCodeNum);
					break;
				case _vga:
					saveCode = pgSystemParam->projectorVgaCode;
					saveCodeNum = &(pgSystemParam->projectorVgaCodeNum);
					break;
				case _hdmi:
					saveCode = pgSystemParam->projectorHdmiCode;
					saveCodeNum = &(pgSystemParam->projectorHdmiCodeNum);
					break;
				default:
					sprintf(logBuf, "unknow actionWay ! actionWay = %d\n", actionWay);	
					writeLog(logBuf, __FILE__, __LINE__);
					return -3;
					break;
		}
		
		unsigned char valueLen = strlen(value);
		char *p = value;
		char *pTmp = NULL;
		unsigned char explainedBytes = 0;
		unsigned char realCodeNum = 0;
		while(explainedBytes < valueLen)
		{
				pTmp = strstr(p, ",");	
				if(!pTmp)
				{
						code[realCodeNum] = a16tou8(p);
						realCodeNum ++;
						explainedBytes += strlen(p) + 1;
						break;
				}
				else
				{
						*pTmp = '\0';
						code[realCodeNum] = a16tou8(p);
						realCodeNum ++;
						explainedBytes += strlen(p) + 1;
						*pTmp = ',';
						p = pTmp + 1;
				}
		}
		
		pthread_mutex_lock(&(pgSystemParam->projectorCodeLock));	
		memcpy(saveCode, code, PROJECTOR_CODE_MAX_LEN);
		*saveCodeNum = realCodeNum;
		pthread_mutex_unlock(&(pgSystemParam->projectorCodeLock));
		
		return 0;
}



int fixConfigFile(const char *saveConfigFile,const char *realConfigFile, const char *saveConfig2File,const char *realConfig2File)
{
#ifdef RECOVER_CONFIG_FILE
		
		if(access(realConfigFile, F_OK) != 0)
		{
			system("wget http://update.incastyun.cn:2680/terminal.config -P /data/edu");
			system("chmod 777 /data/edu/terminal.config");	
		}
#endif

		if(access(saveConfig2File, F_OK) == 0)
		{
				char cmd[256];
				sprintf(cmd, "cp -rf %s %s", saveConfig2File,realConfig2File);	
				system(cmd);
				if(access(realConfig2File, F_OK) == 0)
				{
					sprintf(cmd, "rm %s", saveConfig2File);	
					system(cmd);
				}
		}
	
		if(access(saveConfigFile, F_OK) == 0)
		{
				return combineConfigFile(saveConfigFile, realConfigFile);	
		}
		
		return 0;
}

static int combineConfigFile(const char *saveConfigFile,const char *realConfigFile)
{
		strcpy(pgSystemParam->cLogsDir, defaultLogdir);
		if( pthread_mutex_init (&(pgSystemParam->pmtLogLock),NULL) != 0   ) 
		{
				printf("pthread_mutex_init failure {%s(%d)}\n",__FILE__,__LINE__);	
				return -11;
		}	
		
		LOGD("into combineConfigFile\n");
		
		char logBuf[256];
		if(access(realConfigFile, F_OK) != 0)
		{
				LOGD("terminal.config file lost ! prepare to download a new one !\n");
				system("wget http://update.incastyun.cn:2680/terminal.config -P /data/edu");
		}
		
		ITEM *saveItems;
		saveItems = (ITEM*)malloc(sizeof(ITEM) * maxConfigNum);
		if(saveItems == NULL)
		{
				LOGD("malloc items memory fail !\n");
				return -1;
		}
		
		int realSaveItemNum;
		int ret = file_to_items(saveConfigFile, saveItems, &realSaveItemNum);
		if(ret < 0)
		{
				sprintf(logBuf, "read configure file fail ! ret = %d\n", ret);	
				LOGD(logBuf);
				clearConfigMemory(saveItems, realSaveItemNum);
				return -2;
		}
		
		ITEM *pSaveItem = NULL;
		for(unsigned char i=0; i<realSaveItemNum; i++)
		{
				pSaveItem = saveItems + i;
				if(pSaveItem)
				{
						ret = writeOneConfigValue(pSaveItem->key, pSaveItem->value);	
						if(ret < 0)
						{
								sprintf(logBuf, "write fail ! ret,pSaveItem->key,pSaveItem->value = %d,%s,%s\n", ret,pSaveItem->key,pSaveItem->value);
								LOGD(logBuf);
								clearConfigMemory(saveItems, realSaveItemNum);
								return -3;
						}
				}
				else
				{
						sprintf(logBuf, "memory file data error ! pSaveItem==NULL ! i = %d, realSaveItemNum=%d\n", i,realSaveItemNum);	
						LOGD(logBuf);
						clearConfigMemory(saveItems, realSaveItemNum);
				}
		}
		
		printf("combine ok ! prepare to delete saveConfigFile=%s !\n", saveConfigFile);
		/*char cmd[256];
		sprintf(cmd, "rm -rf %s\n", saveConfigFile);
		system(cmd);*/
		return 0;
}

